﻿//**********************************************************
//Copyright(C)2010 By 波特商网公司 版权所有。
//
//文件名：ucImageButton.cs
//文件功能：自定义按钮控件（加入图片效果）
//
//创建标识：齐李平 || 2011-04-15
//
//修改标识：
//修改描述：
//**********************************************************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using Hmc.IM.Common.Properties;
using System.ComponentModel;

namespace Hmc.IM.Common.UserControls
{
    /// <summary>
    /// 自定义按钮控件（加入图片效果）
    /// </summary>
    public class ucImageButton : Button
    {
        private Image normalBackgroundImage = Resources.button_back;
        /// <summary>
        /// 正常状态背景图片
        /// </summary>
        public Image NormalBackgroundImage
        {
            get { return normalBackgroundImage; }
            set
            {
                this.BackgroundImage = value;
                normalBackgroundImage = value;
            }
        }

        private Image focusBackgroundImage = Resources.button_focus;
        /// <summary>
        /// 获取焦点背景图片
        /// </summary>
        public Image FocusBackgroundImage
        {
            get { return focusBackgroundImage; }
            set { focusBackgroundImage = value; }
        }
        private Image disableBackgroundImage = Resources.button_disable;
        /// <summary>
        /// 禁用状态图片
        /// </summary>
        public Image DisableBackgroundImage
        {
            get { return disableBackgroundImage; }
            set { disableBackgroundImage = value; }
        }

        private Image mouseDownImage;

        /// <summary>
        /// 鼠标点击下的图片
        /// </summary>
        public Image MouseDownImage
        {
            get { return mouseDownImage; }
            set { mouseDownImage = value; }
        }

        private EnumButtonStyle buttonStyle;
        /// <summary>
        /// 按钮样式
        /// </summary>
        public EnumButtonStyle ButtonStyle
        {
            get { return buttonStyle; }
            set
            {
                SetAttarte(value);
                buttonStyle = value;
            }
        }

        /// <summary>
        /// 是否出在Click事件执行中
        /// </summary>
        private bool isClicking = false;

        /// <summary>
        /// 构造函数
        /// </summary>
        public ucImageButton()
        {
            this.FlatStyle = FlatStyle.Flat;
            this.FlatAppearance.BorderSize = 0;
            this.FlatAppearance.BorderColor = Color.White;
            this.FlatAppearance.CheckedBackColor = Color.White;
            this.FlatAppearance.MouseDownBackColor = Color.White;
            this.FlatAppearance.MouseOverBackColor = Color.White;
            this.BackColor = Color.Transparent;
            SetAttarte(EnumButtonStyle.NormalButton);
        }

        /// <summary>
        /// 设置属性
        /// </summary>
        /// <param name="style"></param>
        private void SetAttarte(EnumButtonStyle style)
        {
            switch (style)
            {
                case EnumButtonStyle.None:
                    this.mouseDownImage = null;
                    FocusBackgroundImage = null;
                    DisableBackgroundImage = null;
                    break;
                case EnumButtonStyle.NormalButton:
                    this.Width = 67;
                    this.Height = 22;
                    this.ForeColor = Color.FromArgb(20, 110, 172);
                    this.BackgroundImage = NormalBackgroundImage;
                    this.mouseDownImage = Resources.buttonDown;
                    break;
                case EnumButtonStyle.SearchButton:
                    this.Width = 77;
                    this.Height = 22;
                    NormalBackgroundImage = Resources.search_in;
                    this.BackgroundImage = NormalBackgroundImage;
                    FocusBackgroundImage = Resources.search_on;
                    DisableBackgroundImage = Resources.search_out;
                    mouseDownImage = Resources.SearchDown;
                    break;
                case EnumButtonStyle.DialogButton:
                    this.Width = 67;
                    this.Height = 21;
                    NormalBackgroundImage = Resources.in2;
                    this.BackgroundImage = NormalBackgroundImage;
                    FocusBackgroundImage = Resources.on2;
                    DisableBackgroundImage = Resources.out2;
                    mouseDownImage = Resources.onDown;
                    break;
            }
        }

        /// <summary>
        /// 重写获取焦点事件
        /// </summary>
        /// <param name="e"></param>
        protected override void OnGotFocus(EventArgs e)
        {
            //base.OnGotFocus(e);
            if (this.Enabled == true)
            {
                this.FlatStyle = FlatStyle.Standard;
                if(FocusBackgroundImage!=null)
                this.BackgroundImage = FocusBackgroundImage;
            }
        }

        /// <summary>
        /// 重写失去焦点事件
        /// </summary>
        /// <param name="e"></param>
        protected override void OnLostFocus(EventArgs e)
        {
            base.OnLostFocus(e);
            if (this.Enabled == true)
            {
                this.FlatStyle = FlatStyle.Flat;
                if(NormalBackgroundImage != null)
                this.BackgroundImage = NormalBackgroundImage;
            }
            System.Diagnostics.Debug.WriteLine(string.Format("{0}:OnLostFocus释放", DateTime.Now));
        }

        /// <summary>
        /// 重写禁用启用改变事件
        /// </summary>
        /// <param name="e"></param>
        protected override void OnEnabledChanged(EventArgs e)
        {
            base.OnEnabledChanged(e);
            if (this.Enabled == false)
            {
                if(DisableBackgroundImage != null)
                this.BackgroundImage = DisableBackgroundImage;
            }
            else
            {
                if (NormalBackgroundImage != null) this.BackgroundImage = NormalBackgroundImage;
            }
        }

        /// <summary>
        /// 鼠标移入事件
        /// </summary>
        /// <param name="e"></param>
        protected override void OnMouseEnter(EventArgs e)
        {
            base.OnMouseEnter(e);
            if (this.Enabled == true)
            {
                if(FocusBackgroundImage != null)
                this.BackgroundImage = FocusBackgroundImage;
            }
        }

        /// <summary>
        /// 鼠标移出事件
        /// </summary>
        /// <param name="e"></param>
        protected override void OnMouseLeave(EventArgs e)
        {
            base.OnMouseLeave(e);
            if (this.isClicking)
            {
                this.BeginInvoke(new Action<Image>(SetBackgroundImage), MouseDownImage);
            }
            else
            {
                if (this.Enabled == true)
                {
                    if (NormalBackgroundImage != null)
                        this.BackgroundImage = NormalBackgroundImage;
                }
            }
            System.Diagnostics.Debug.WriteLine(string.Format("{0}:OnMouseLeave释放", DateTime.Now));
        }

        /// <summary>
        /// 鼠标弹起事件
        /// </summary>
        /// <param name="mevent"></param>
        protected override void OnMouseUp(MouseEventArgs mevent)
        {
            base.OnMouseUp(mevent);
            if (this.Enabled == true)
            {
                if(NormalBackgroundImage!= null)
                this.BackgroundImage = NormalBackgroundImage;
                System.Diagnostics.Debug.WriteLine(string.Format("{0}:释放", DateTime.Now));
            }
        }

        /// <summary>
        /// 鼠标按下时事件
        /// </summary>
        /// <param name="mevent"></param>
        protected override void OnMouseDown(MouseEventArgs mevent)
        {
            if (mouseDownImage != null)
            {
                if (this.Enabled) this.BackgroundImage = MouseDownImage;
                System.Diagnostics.Debug.WriteLine(string.Format("{0}:按下",DateTime.Now));
            }
            base.OnMouseDown(mevent);
        }

        protected override void OnClick(EventArgs e)
        {
            this.isClicking = true;
            base.OnClick(e);
            this.isClicking = false;
        }

        private void SetBackgroundImage(Image img)
        {
            if (this.Enabled == true)
            {
                if (img != null)
                    this.BackgroundImage = img;
                System.Diagnostics.Debug.WriteLine(string.Format("{0}:Click释放", DateTime.Now));
            }
        }
    }

    /// <summary>
    /// 按钮枚举
    /// </summary>
    public enum EnumButtonStyle
    {
        /// <summary>
        /// 无
        /// </summary>
        None,
        /// <summary>
        /// 普通按钮
        /// </summary>
        NormalButton,
        /// <summary>
        /// 搜索按钮
        /// </summary>
        SearchButton,
        /// <summary>
        /// 弹出层按钮
        /// </summary>
        DialogButton
    }
}
